/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：ReqCfgController.java
 * History:
 *         2012-6-7: Initially created, Chrise.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.SysRequestConfig;
import com.xteam.ecsystem.service.admin.ReqCfgService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 请求配置控制层。
 * @author Chrise
 */
@Controller
public class ReqCfgController {
	
	@Autowired
	private ReqCfgService rcService;
	
	@Autowired
	private SystemLogService logService;
	
	/**
	 * 获取请求配置。
	 * @author Chrise 2012-6-7
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getRequestConfigs.do")
	public void getRequestConfigs(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String srcName = ReqHelper.getRequestString(request, "srcName");
		int srcState = ReqHelper.getRequestInt(request, "srcState");
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		
		PageCenterStation<SysRequestConfig> pcs = this.rcService.getRequestConfigs(srcName, srcState, pageIndex, pageSize);
		JSONObject result = JSONObject.fromObject(pcs);
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 切换请求配置状态。
	 * @author Chrise 2012-6-8
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/switchRcState.do")
	public void switchRcState(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		JSONObject object = JSONObject.fromObject(data);
		
		this.rcService.switchState(object.getString("srcId"), object.getBoolean("srcTo"));
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_REQCFG_MGR, 
				"switchState", ConstantLocation.LOG_Edit_STATE, "SysRequestConfig");
	}
	
	/**
	 * 删除请求配置。
	 * @author Chrise 2012-6-11
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/delRequestConfigs.do")
	public void delRequestConfigs(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		JSONArray array = JSONArray.fromObject(data);
		
		List<String> ids = new ArrayList<String>();
		for (int index = 0; index < array.size(); index ++) {
			JSONObject object = array.getJSONObject(index);
			ids.add(object.getString("srcId"));
		}
		
		this.rcService.delRequestConfigs(ids);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_REQCFG_MGR, 
				"delRequestConfigs", ConstantLocation.LOG_DELETE_CFG, "SysRequestConfig");
	}
	
	/**
	 * 保存请求配置。
	 * @author Chrise 2012-6-12
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/saveRequestConfig.do")
	public void saveRequestConfig(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD_CFG;
		
		JSONObject object = JSONObject.fromObject(data);
		SysRequestConfig src = (SysRequestConfig)JSONObject.toBean(object, SysRequestConfig.class);
		
		JSONObject result = new JSONObject();
		if (this.rcService.saveRequestConfig(src)) {
			result.put("issuccess", true);
			
			if(StringUtil.isNotNullOrEmpty(object.get("srcId"))){
				lslType = ConstantLocation.LOG_EDIT_CFG;
			}
			
			this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_REQCFG_MGR, 
					"saveRequestConfig", lslType, "SysRequestConfig");
		} else {
			result.put("issuccess", false);
			result.put("message", "请求路径对应的配置已存在！");
		}
		
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取请求配置。
	 * @author Chrise 2012-6-12
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getRequestConfig.do")
	public void getRequestConfig(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		JSONObject object = JSONObject.fromObject(data);
		
		SysRequestConfig src = this.rcService.getRequestConfig(object.getString("srcId"));
		
		JSONObject result = new JSONObject();
		if (src != null) {
			JSONObject bean = JSONObject.fromObject(src);
			
			result.put("issuccess", true);
			result.put("message", bean.toString());
		} else {
			result.put("issuccess", false);
			result.put("message", "指定配置不存在！");
		}
		
		response.getWriter().write(result.toString());
	}
}
